package object_oriented

class Temp {
  var x = 0

  def x(i: Int): Int = {
    x = x + i
    x
  }

  def x(i: Int, j: Int): Int = {
    val temp = i + j
    x += temp
    x
  }

  // 实现求2的从a到b次幂的和
  def sumPowersOfTwo(a: Int, b: Int): Int = {
    // 实现求2的整数次幂
    // 嵌套方法仅在其定义的方法里面可见
    def powerOfTow(x: Int): Int = {
      if (x == 0) 1 else 2 * powerOfTow(x-1)
    }
    if (a > b) 0 else powerOfTow(a) + sumPowersOfTwo(a+1, b)
  }
}

object Temp {
  def main(args: Array[String]): Unit = {
    val temp = new Temp
    temp.x
    temp x 4
    temp.x(5, 6)
    temp

    // 2^2 + 2^3 + 2^4 = 4 + 8 + 16 = 28
    var value = temp.sumPowersOfTwo(2, 4)
    // 2^2 + 2^3 + 2^4 + 2^5 = 4 + 8 + 16 + 32 = 60
    value = temp.sumPowersOfTwo(2, 5)
    // 2^1 + 2^2 +2^3 = 14
    value = temp.sumPowersOfTwo(1, 3)
    value
  }
}
